﻿<UserControl x:Class="SQLDeploymentBuilder.UI.Controls.Environments.EnvironmentEditor"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:file="clr-namespace:SQLDeployAnything.Data.Files;assembly=SQLDeployAnything.Data"
             xmlns:converters="clr-namespace:SQLDeploymentBuilder.UI.Converters"
             xmlns:ConnectionList ="clr-namespace:SQLDeploymentBuilder.UI.Controls.Connections"
             xmlns:ConnectionEdit="clr-namespace:SQLDeploymentBuilder.UI.Controls.Connections.Editors"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        
        <converters:IsViewVisableConverter x:Key="VisabilityConverter"/>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto" MinHeight="150"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" Margin="4">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <Label Grid.Column="0" Grid.Row="0" Content="Connections File:"/>
            <Grid Grid.Column="1" Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBox Name="txt_ConnectionFile" Grid.Column="0" Grid.Row="0" Margin="4" 
                     Text="{Binding Path=ConnectionFile, BindsDirectlyToSource=True, 
                        Mode=TwoWay,
                        NotifyOnSourceUpdated=True,
                        NotifyOnTargetUpdated=True}"/>
                <Button Grid.Column="1" Grid.Row="0" Content="..." Margin="4" Click="Open_Click" />

            </Grid>

            <Label Grid.Column="0" Grid.Row="1" Content="Selected Environment:"/>

            <Grid Grid.Column="1" Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <ComboBox Name="cmb_SelectedEnvironment" Grid.Column="0" Grid.Row="0" Margin="4"
                          ItemsSource="{Binding Path=Environments, NotifyOnSourceUpdated=True, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"
                          DisplayMemberPath="Name"
                          />
                <Button Name="AddButton" Content="New..." Grid.Column="1" Grid.Row="0" Margin="4,4,4,0" Height="22" 
                        IsEnabled="{Binding ElementName=InputBox, Path=Visibility, Mode=TwoWay,
                            Converter={StaticResource VisabilityConverter}}"
                        >
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="Button.Click" SourceName="AddButton" >
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="InputBox" Storyboard.TargetProperty="(UIElement.Visibility)">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Button.Triggers>
                </Button>

                <!-- It's important that this is in the end of the XAML as it needs to be on top of everything else! -->
                <Grid x:Name="InputBox" Visibility="Collapsed">
                    <StackPanel>
                        <TextBlock Margin="5" Text="New Environment Name:" FontWeight="Bold" FontFamily="Cambria" />
                        <TextBox MinWidth="150" HorizontalAlignment="Center" x:Name="InputTextBox" Margin="4"/>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <Button x:Name="YesButton" Margin="5" Content="Create" 
                                        Command="{Binding AddEnvironmentCommand}" 
                                        CommandParameter="{Binding Text, ElementName=InputTextBox, Mode=TwoWay}">
                                <Button.Triggers>
                                    <EventTrigger RoutedEvent="Button.Click" SourceName="YesButton" >
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="InputBox" Storyboard.TargetProperty="(UIElement.Visibility)">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </Button.Triggers>
                            </Button>
                            <Button x:Name="NoButton" Margin="5" Content="Cancel" >
                                <Button.Triggers>
                                    <EventTrigger RoutedEvent="Button.Click" SourceName="NoButton" >
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="InputBox" Storyboard.TargetProperty="(UIElement.Visibility)">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </Button.Triggers>
                            </Button>
                        </StackPanel>
                    </StackPanel>
                </Grid>
                
                
            </Grid>
        </Grid>

        <StackPanel Orientation="Vertical" Grid.Row="1">
            <Label Content="Environment Connections:"/>
            <ConnectionList:ConnectionList x:Name="lsv_Connections"  
                                           Margin="4" MinHeight="150" 
                                           DataContext="{Binding SelectedValue, ElementName=cmb_SelectedEnvironment, 
                                                         NotifyOnSourceUpdated=True}"/>
        </StackPanel>




    </Grid>
    
    

</UserControl>
